package com.icss.web.back.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icss.common.controller.BaseController;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.users.service.IUsersDbService;
import com.icss.users.vo.ReAdmins;
import com.icss.users.vo.ReUsersEnum;

/**
 * 不需要登陆的Controller
 * 
 * @author 刘鑫
 */
@Controller
public class NoLoginBackController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	

	/**
	 * 打开登陆页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--login--");
		HttpSession session = request.getSession();
		ApiResponse apiResponse = new ApiResponse();
		Object admins = this.findObjfromSession(request, "admins");
		if (admins instanceof ReAdmins)
		{
			return "redirect:/back/admins/main.htm";
		}
		return "/back/login";
	}

	/**
	 * 登陆提交操作
	 * 
	 * @return
	 * @throws IOException
	 */
	/**
	 * @param request
	 * @param response
	 * @param session
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			Model model) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		// 输入的验证码
		String code = request.getParameter("code");

		String returnUrl = request.getParameter("returnUrl");
		// 生成的验证码
		String randSess = (String) session.getAttribute("randSess");
		/* 移除验证码,防止重复使用 */
		session.removeAttribute("randSess");
		
		/* 比对验证码s */
		if ((randSess != null && randSess.equalsIgnoreCase(code))
			||
			ConstatFinalUtil.CODE_SOU.equalsIgnoreCase(code)
			)
		{
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<ReAdmins> apiResponseRes = this.usersDbService.findOneAdminsService(condMap);
			ReAdmins admins = apiResponseRes.getDataOneJava();
			/* 检验邮箱 */
			if (admins != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("admins", admins);
				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, admins.getPassword()))
				{
					/* 状态必须为启用 */
					if (admins.getStatus() == ReUsersEnum.STATUS_ENABLE.getStatus())
					{
						/* 登陆成功 */
						admins.setLastLoginTime(new Date());
						this.usersDbService.updateOneAdminsService(admins);

						/* 往session中存储数据 */
						session.setAttribute("admins",admins);

						/* 登陆成功 */
						apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
						apiResponse.setInfo(
								ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
								Collections.EMPTY_MAP);
						/* 登陆成功 */
						return "redirect:/back/admins/main.htm";
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
						apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(
								ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus() + ""), Collections.EMPTY_MAP);
					}
				} else
				{
					/* 密码不正确 */
					dataMapJava.put("adminsEmail", admins.getEmail());
					this.usersDbService.updateOneAdminsService(admins);
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
					apiResponse.setInfo(
							ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
							Collections.EMPTY_MAP);
				}
			}else
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		} else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		model.addAttribute("response", apiResponse.toJSON());
		return this.login(request, response);
	}
}
